package com.hsb.controller;

import com.hsb.entity.Book;
import com.hsb.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookService bookService;

    //增加一条记录
    @PostMapping("/insert/{name}/{count}/{detail}")
    public Boolean insert(@PathVariable("name") String name,@PathVariable("count") Integer count,@PathVariable("detail") String detail) {
        int i = bookService.insert(new Book(name,count,detail));
        return i > 0;
    }

    //根据id删除记录
    @DeleteMapping("/delete/{id}")
    public Boolean deleteById(@PathVariable Integer id) {
        int i = bookService.deleteById(id);
        return i > 0;
    }

    //根据id查询记录
    @GetMapping("/query/{id}")
    public Book queryById(@PathVariable Integer id) {
        return bookService.queryById(id);
    }

    //查询所有记录
    @GetMapping("/queryAll")
    public List<Book> queryAll() {
        return bookService.queryAll();
    }

    //根据id修改记录
    @PutMapping("/update/{id}/{name}/{count}/{detail}")
    public Boolean update(@PathVariable("id") Integer id,@PathVariable("name") String name,@PathVariable("count") Integer count,@PathVariable("detail") String detail) {
        int i = bookService.updateById(new Book(id,name,count,detail));
        return i > 0;
    }
}